/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package towerdefence.model;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import towerdefence.model.event.BuilderDiedEvent;
import towerdefence.model.event.Event;
import towerdefence.model.event.EventListener;
import towerdefence.model.event.IncomeEvent;
import towerdefence.model.event.IncomeRaisedEvent;
import towerdefence.model.event.OutOfMoneyEvent;
import towerdefence.model.event.PlayerDiedEvent;

/**
 *
 * @author Gussoh
 */
public class Player {

    protected  double money;
    protected  Model model;
    protected  double income;
    protected  List<EventListener> eventListeners = new ArrayList<EventListener>();
    protected  Color color;
    
    protected  Set<Block> blocks = new HashSet<Block>();
    protected  Set<Cannon> cannons = new HashSet<Cannon>();
    protected  Set<Builder> builders = new HashSet<Builder>();
    

    public Player(Model model, Color color, double money, double income) {
        this.model = model;
        this.money = money;
        this.income = income;
        this.color = color;
    }
    
    protected void addBuilder(Builder b) {
        builders.add(b);
        model.addBody(b);
    }

    public void addBlock(Block block) {
        if(tryPurchase(block)) {
            blocks.add(block);
            model.addBody(block);
        }
    }
    
    public void addEventListener(EventListener e) {
        eventListeners.add(e);
    }
    
    public void removeEventListener(EventListener e) {
        eventListeners.remove(e);
    }

    protected void builderDied(Builder builder) {
        builders.remove(builder);
        if(builders.isEmpty()) {
            eventOccured(new PlayerDiedEvent());
            model.playerDied(this);
        } else {
            eventOccured(new BuilderDiedEvent());
        }
    }
    
    private void eventOccured(Event e) {
        for (EventListener eventListener : eventListeners) {
            eventListener.eventOccured(e);
        }
    }
    
    public void addCannon(Cannon cannon) {
        if (tryPurchase(cannon)) {
            cannons.add(cannon);
            model.addBody(cannon);
        }
    }
    
    public void shoot(Cannon c) {
        CannonBall cb = c.getCannonBall();
        if(tryPurchase(cb)) {
            model.addCannonBall(cb);
            c.shoot(cb);
            raiseIncome(cb);
        }
    }

    public void addIncome() {
        money += income;
        eventOccured(new IncomeEvent(income));
    }
    
    public void addMoneyFrom(Purchaseable item) {
        double amount = item.getPrice() / 10;
        money += amount;
        eventOccured(new IncomeEvent(amount));
    }
    
    private void raiseIncome(Purchaseable withThisItem) {
        double amount = withThisItem.getPrice() / 100;
        income += amount;
        eventOccured(new IncomeRaisedEvent(amount));
    }
    
    public boolean canPurchase(Purchaseable item) {
        return money - item.getPrice() > 0;
    }
    
    public boolean tryPurchase(Purchaseable item) {
        if (money - item.getPrice() < 0) {
            eventOccured(new OutOfMoneyEvent());
            return false;
        } else {
            money -= item.getPrice();
            return true;
        }
    }

    public Set<Block> getBlocks() {
        return blocks;
    }

    public Set<Cannon> getCannons() {
        return cannons;
    }

    public Set<Builder> getBuilders() {
        return builders;
    }

    public Color getColor() {
        return color;
    }

    
    
    
}
